package com.tenbent.product.center.example.stu.action;

import java.util.*;

import com.tenbent.product.base.cache.Caches;
import com.tenbent.product.base.constants.WebConstans;
import com.tenbent.product.base.redis.RedisCacheService;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tenbent.product.base.action.BaseController;
import com.tenbent.product.base.query.page.PageResult;
import com.tenbent.product.base.query.page.Pageable;
import com.tenbent.product.center.example.stu.bo.Student;
import com.tenbent.product.center.example.stu.service.StudentService;

/**
 * Created by ThinkPad on 2017/7/27.
 */
@Controller
public class StudentController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(StudentController.class);

	public static class Url {
		public static final String save = "/eg/student/save";
		public static final String update = "/eg/student/update";
		public static final String delete = "/eg/student/delete";
		public static final String deletes = "/eg/student/deletes";
		public static final String query = "/eg/student/query";
		public static final String queryList = "/eg/student/query_list";
		public static final String queryObj = "/eg/student/query_list_obj";
		public static final String queryObjToMap = "/eg/student/query_list_obj_map";
		public static final String queryMap = "/eg/student/query_list_map";
		public static final String queryMapToMap = "/eg/student/query_list_map_map";
		public static final String queryAll = "/eg/student/query_all";
		public static final String queryStuPage = "/eg/student/query_stu_page";
		public static final String queryStuPageMap = "/eg/student/query_stud_page_map";

		public static final String queryPageObj = "/eg/student/query_page_obj";
		public static final String queryPageObjToMap = "/eg/student/query_page_obj_map";

		public static final String queryPageMap = "/eg/student/query_page_map";
		public static final String queryPageMapToMap = "/eg/student/query_page_map_to_map";

		// ======================== 以下测试缓存 ==============================//
		public static final String saveStu = "/eg/student/save_stu";
		public static final String updateStu = "/eg/student/update_stu";
		public static final String removeStu = "/eg/student/remove_stu";
		public static final String findOne = "/eg/student/find_one";

	}

	@Autowired
	@Qualifier("studentServiceImpl")
	private StudentService studentService;

	@Autowired
	@Qualifier("redisCacheServiceImpl")
	private RedisCacheService redisCacheService;

	@RequestMapping(value = Url.save, method = { RequestMethod.POST, RequestMethod.GET })
	public String save(@ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId, Model model) {

		Student stu = new Student();
		stu.setName("niubi1001");
		stu.setAge(40);
		stu.setDeptId(9);
		stu.setCreateId(managerId);
		stu.setModifyId(managerId);
		studentService.add(stu);
		model.addAttribute("stu", stu);
		return "/eg/success";
	}

	@RequestMapping(value = Url.update, method = { RequestMethod.POST, RequestMethod.GET })
	public String update(@ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId, Model model) {

		// Student stu = studentService.getByKey(179);
		Student stu = new Student();
		stu.setId(179);
		stu.setName("haha3");
		stu.setAge(37);
		stu.setDeptId(4);
		stu.setModifyId(managerId);
		studentService.update(stu);
		model.addAttribute("stu", stu);
		return "/eg/success";
	}

	@RequestMapping(value = Url.delete, method = { RequestMethod.POST, RequestMethod.GET })
	public String delete(@ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {

		Student stu = new Student();
		stu.setId(195);
		stu.setModifyId(managerId);
		// studentService.delete(stu);
		studentService.deleteLogic(stu);

		return "/eg/success";
	}

	@RequestMapping(value = Url.deletes, method = { RequestMethod.POST, RequestMethod.GET })
	public String deletes(@ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {

		Integer[] ids = new Integer[] { 2, 3 };
		Integer[] ids2 = new Integer[] { 4, 8 };
		studentService.deleteLogicByIds(ids, managerId);
		studentService.deleteByIds(ids2, managerId);
		return "/eg/success";
	}

	@RequestMapping(value = Url.query, method = { RequestMethod.POST, RequestMethod.GET })
	public String queryStu(Model model) {

		Student stu = studentService.getByKey(2);
		model.addAttribute("stu", stu);
		return "/eg/success";
	}

	@RequestMapping(value = Url.queryList, method = { RequestMethod.POST, RequestMethod.GET })
	public String queryList(Model model) {

		Student stu = new Student();
		stu.setAge(30);
		List<Student> stus = studentService.queryStuList(stu);
		model.addAttribute("stus", stus);
		return "/eg/success";
	}

	@RequestMapping(value = Url.queryStuPage, method = { RequestMethod.POST, RequestMethod.GET })
	public String queryStuPage(Model model) {

		Student stu = new Student();
		stu.setAge(30);
		Pageable pageable = new Pageable(1, 2);
		pageable.setParamBizObject(stu);
		PageResult<Student> pageResult = studentService.queryStuPage(pageable);
		Long total = pageResult.getTotal();
		Integer totalPage = pageResult.getTotalPages();
		model.addAttribute("page", pageResult);
		model.addAttribute("total", total);
		model.addAttribute("totalPage", totalPage);
		return "/eg/success";
	}

	@RequestMapping(value = Url.queryStuPageMap, method = { RequestMethod.POST, RequestMethod.GET })
	public String queryStuPageMap(Model model) {

		Student stu = new Student();
		stu.setAge(30);
		Pageable pageable = new Pageable(1, 2);
		pageable.setParamBizObject(stu);
		PageResult<Map<String, Object>> pageResult = studentService.queryStuPageMap(pageable);
		Long total = pageResult.getTotal();
		Integer totalPage = pageResult.getTotalPages();
		model.addAttribute("page", pageResult);
		model.addAttribute("total", total);
		model.addAttribute("totalPage", totalPage);
		return "/eg/success";
	}

	@RequestMapping(value = Url.queryAll, method = { RequestMethod.POST, RequestMethod.GET })
	public String queryAll(Model model) {

		List<Student> stus = studentService.getAll();
		model.addAttribute("stus", stus);
		return "/eg/success";
	}

	@RequestMapping(value = Url.queryObj, method = { RequestMethod.POST, RequestMethod.GET })
	public String queryObj(Model model) {

		Student stu = new Student();
		stu.setAge(30);
		stu.setName("yan");
		List<Student> stus = studentService.queryObj(stu);
		model.addAttribute("stus", stus);
		return "/eg/success";
	}

	@RequestMapping(value = Url.queryObjToMap, method = { RequestMethod.POST, RequestMethod.GET })
	public String queryObjToMap(Model model) {

		Student stu = new Student();
		stu.setAge(30);
		stu.setName("yan");
		List<Map<String, Object>> stus = studentService.queryObjToMap(stu);
		model.addAttribute("stus", stus);
		return "/eg/success";
	}

	@RequestMapping(value = Url.queryMap, method = { RequestMethod.POST, RequestMethod.GET })
	public String queryMap(Model model) {

		Map<String, Object> paramMap = new HashedMap();
		paramMap.put("age", 30);
		paramMap.put("name", "kai");
		List<Student> stus = studentService.queryMap(paramMap);
		model.addAttribute("stus", stus);
		return "/eg/success";
	}

	@RequestMapping(value = Url.queryMapToMap, method = { RequestMethod.POST, RequestMethod.GET })
	public String queryMapToMap(Model model) {

		Map<String, Object> paramMap = new HashedMap();
		paramMap.put("age", 30);
		paramMap.put("name", "kai");
		List<Map<String, Object>> stus = studentService.queryMapToMap(paramMap);
		model.addAttribute("stus", stus);
		return "/eg/success";
	}

	@RequestMapping(value = Url.queryPageObj, method = { RequestMethod.POST, RequestMethod.GET })
	public String queryPageObj(Model model) {

		Student stu = new Student();
		stu.setAge(30);
		Pageable pageable = new Pageable(1, 2);
		pageable.setParamBizObject(stu);
		PageResult<Student> pageResult = studentService.queryPageObj(pageable);
		Long total = pageResult.getTotal();
		Integer totalPage = pageResult.getTotalPages();
		Student stu2 = pageResult.get(0);
		model.addAttribute("page", pageResult);
		model.addAttribute("total", total);
		model.addAttribute("totalPage", totalPage);
		return "/eg/success";
	}

	@RequestMapping(value = Url.queryPageObjToMap, method = { RequestMethod.POST, RequestMethod.GET })
	public String queryPageObjToMap(Model model) {

		Student stu = new Student();
		stu.setAge(30);
		Pageable pageable = new Pageable(1, 2);
		pageable.setParamBizObject(stu);
		PageResult<Map<String, Object>> pageResult = studentService.queryPageObjToMap(pageable);
		Long total = pageResult.getTotal();
		Integer totalPage = pageResult.getTotalPages();
		Map<String, Object> stuMap = pageResult.get(0);
		model.addAttribute("page", pageResult);
		model.addAttribute("total", total);
		model.addAttribute("totalPage", totalPage);
		return "/eg/success";
	}

	@RequestMapping(value = Url.queryPageMap, method = { RequestMethod.POST, RequestMethod.GET })
	public String queryPageMap(Model model) {

		Map<String, Object> paramMap = new HashedMap();
		paramMap.put("age", 30);
		Pageable pageable = new Pageable(1, 2);
		pageable.setParamMap(paramMap);
		PageResult<Student> pageResult = studentService.queryPageMap(pageable);
		Long total = pageResult.getTotal();
		Integer totalPage = pageResult.getTotalPages();
		Student stu = pageResult.get(0);
		model.addAttribute("page", pageResult);
		model.addAttribute("total", total);
		model.addAttribute("totalPage", totalPage);
		return "/eg/success";
	}

	@RequestMapping(value = Url.queryPageMapToMap, method = { RequestMethod.POST, RequestMethod.GET })
	public String queryPageMapToMap(Model model) {

		Map<String, Object> paramMap = new HashedMap();
		paramMap.put("age", 30);
		Pageable pageable = new Pageable(1, 2);
		pageable.setParamMap(paramMap);
		PageResult<Map<String, Object>> pageResult = studentService.queryPageMapToMap(pageable);
		Long total = pageResult.getTotal();
		Integer totalPage = pageResult.getTotalPages();
		Map<String, Object> stu = pageResult.get(0);
		model.addAttribute("page", pageResult);
		model.addAttribute("total", total);
		model.addAttribute("totalPage", totalPage);
		return "/eg/success";
	}

	// =================================以下为测试缓存=============================================//

	@RequestMapping(value = Url.saveStu, method = { RequestMethod.POST, RequestMethod.GET })
	public String saveStu(@ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId, Model model) {

		Student stu = new Student();
		stu.setName("缓存9999");
		stu.setAge(40);
		stu.setDeptId(9);
		stu.setDelFlag(0);
		stu.setCreateId(managerId);
		stu.setModifyId(managerId);
		stu.setCreateDate(new Date());
		stu.setModifyDate(new Date());
		stu = studentService.saveStu(stu);
		// 使用redis做缓存时在放开注解
		// redisCacheService.set("key_yan", "kaiwei.yan");
		// redisCacheService.set("key_yan_obj", stu);
		// List<Student> list = new ArrayList<>();
		// list.add(stu);
		// redisCacheService.lPush("key_yan_list",list);
		//
		// String value = (String) redisCacheService.get("key_yan");
		// Student resultObj = (Student) redisCacheService.get("key_yan_obj");
		// List<Student> resutlList = (List<Student>)
		// redisCacheService.lRange("key_yan_list", 0, 100);
		model.addAttribute("stu", stu);
		return "/eg/success";
	}

	@RequestMapping(value = Url.updateStu, method = { RequestMethod.POST, RequestMethod.GET })
	public String updateStu(@ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId, Model model) {

		Student stu = studentService.findOne(228);
		stu.setName("修改缓存8888");
		stu.setAge(37);
		stu.setDeptId(4);
		stu.setModifyId(managerId);
		stu = studentService.updateStu(stu);
		model.addAttribute("stu", stu);
		return "/eg/success";
	}

	@RequestMapping(value = Url.removeStu, method = { RequestMethod.POST, RequestMethod.GET })
	public String deleteStu(Integer id) {
		studentService.removeStu(id);
		return "/eg/success";
	}

	@RequestMapping(value = Url.findOne, method = { RequestMethod.POST, RequestMethod.GET })
	public String findOne(Integer id, Model model) {
		Student stu = studentService.findOne(id);
		model.addAttribute("stu", stu);
		return "/eg/success";
	}

}
